#if !defined(_DEBUG_H)
#define _DEBUG_H

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

extern int log_fd;
#define LOG_FILE	"log.txt"

#define Debug(log)	\
{	\
	char src_pts[512];	\
	char stamp[1024];	\
	time_t now;	\
	struct tm *timenow;	\
	if(log_fd == 0)	\
	{	\
		if((log_fd = ::open(LOG_FILE,O_WRONLY | O_APPEND))<0)	\
		{							\
			if((log_fd= open(LOG_FILE,O_CREAT | O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR))<0)	\
			{							\
				printf("Can not open log file\n");		\
				exit(1);	\
			}	\
		}	\
		printf("Open the log file.\n");	\
	}	\
	time(&now);	\
	timenow = localtime(&now);	\
	strftime(stamp,sizeof(stamp),"%T %x",timenow);	\
	sprintf(src_pts," File: %s, Func: %s, Line: %d\n",__FILE__,__func__,__LINE__);	\
	strcat(stamp,src_pts);	\
	write(log_fd,stamp,strlen(stamp));	\
	printf(stamp);	\
	write(log_fd,log,strlen(log));	\
	printf((char *)log);	\
}

#endif
